package com.lagou.sqlSession;

import com.lagou.pojo.Configuratioin;
import com.lagou.pojo.MappedStatement;

import java.lang.reflect.*;
import java.util.List;
import java.util.Objects;

/**
 * @Description:
 * @Author: yw
 * @Date: 2021/7/25 10:50
 **/
public class DefaultSqlSession implements SqlSesstion {
    private Configuratioin configuratioin;

    private SimpleExecutor simpleExecutor;

    public DefaultSqlSession(Configuratioin configuratioin) {
        this.configuratioin = configuratioin;
        this.simpleExecutor = new SimpleExecutor();
    }

    @Override
    public <E> List<E> selectList(String statementId, Object... params) throws Exception {
        MappedStatement mappedStatement = configuratioin.getMappedStatementMap().get(statementId);
        List<Object> query = simpleExecutor.query(configuratioin, mappedStatement, params);
        return (List<E>) query;
    }

    @Override
    public <T> T selectOne(String statementId, Object... params) throws Exception {
        List<Object> objects = selectList(statementId, params);
        if(objects.size() == 1){
            return (T) objects.get(0);
        }else{
            throw new RuntimeException("返回的结果为，或者数量大于1");
        }
    }

    @Override
    public int update(String statementId, Object... params) throws Exception {
        MappedStatement mappedStatement = configuratioin.getMappedStatementMap().get(statementId);
        return simpleExecutor.saveOrUpdate(configuratioin,mappedStatement,params);
    }

    @Override
    public int insert(String statementId, Object... params) throws Exception {
        MappedStatement mappedStatement = configuratioin.getMappedStatementMap().get(statementId);
        return simpleExecutor.saveOrUpdate(configuratioin,mappedStatement,params);
    }

    @Override
    public int delete(String statementId, Object... params) throws Exception {
        MappedStatement mappedStatement = configuratioin.getMappedStatementMap().get(statementId);
        return simpleExecutor.saveOrUpdate(configuratioin,mappedStatement,params);
    }

    @Override
    public <T> T getMapper(Class<?> mapperClass) {
        Object proxyInstance = Proxy.newProxyInstance(DefaultSqlSession.class.getClassLoader(), new Class[]{mapperClass}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                String methodName = method.getName();
                String className = method.getDeclaringClass().getName();
                String statementId = className+"."+methodName;
                //根据不同的参数sql类型来返回不同的调用反复
                MappedStatement mappedStatement = configuratioin.getMappedStatementMap().get(statementId);
                switch (mappedStatement.getSqlTypeEnum()){
                    case INSERT:{
                        return insert(statementId,args);
                    }
                    case DELETE:{
                        return delete(statementId,args);
                    }
                    case UPDATE:{
                        return update(statementId,args);
                    }
                    case SELECT:{
                        //根据不同的返回值类型来确定调用的方法
                        AnnotatedType returnType = method.getAnnotatedReturnType();
                        if(returnType instanceof ParameterizedType){
                            return selectList(statementId,args);
                        }
                        return selectOne(statementId,args);
                    }
                }
               return null;
            }
        });
        return (T) proxyInstance;
    }
}
